﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLTV
{
    public partial class frmQuanLySach : Form
    {
        public frmQuanLySach()
        {
            InitializeComponent();
        }

        private void frmTiepNhanSachMoi_Load(object sender, EventArgs e)
        {
            grbQuanLySach.Enabled = false;
            btnThucHien.Enabled = false;
            btnNhapLai.Enabled = false;
        }

        private void LayDanhSachTheLoai()
        {
            List<TheLoaiDTO> listTheLoai = TheLoaiService.LayDanhSachTheLoai();
            cmbTheLoai.DataSource = listTheLoai;
            cmbTheLoai.DisplayMember = "TenTheLoai";
            cmbTheLoai.ValueMember = "MaTheLoai";
        }

        private void LayDanhSachTacGia()
        {
            List<TacGiaDTO> listTacGia = TacGiaService.LayDanhSachTacGia();
            cmbTacGia.DataSource = listTacGia;
            cmbTacGia.DisplayMember = "TenTacGia";
            cmbTacGia.ValueMember = "MaTacGia";
        }

        private void LayDanhSachNXB()
        {
            List<NhaXuatBanDTO> listNXB = NhaXuatBanServices.LayDanhSachNXB();
            cmbNXB.DataSource = listNXB;
            cmbNXB.DisplayMember = "TenNhaXuatBan";
            cmbNXB.ValueMember = "MaNhaXuatBan";
        }

        private void rdbTiepNhan_CheckedChanged(object sender, EventArgs e)
        {
            grbQuanLySach.Enabled = true;
            btnThucHien.Enabled = true;
            btnNhapLai.Enabled = true;

            txtMaSoSach.ReadOnly = true;
            txtTenSach.ReadOnly = false;
            txtNamXB.ReadOnly = false;
            txtTriGia.ReadOnly = false;
            cmbNXB.Enabled = true;
            cmbTacGia.Enabled = true;
            cmbTheLoai.Enabled = true;

            txtMaSoSach.Text = (SachService.LayMaSachMax() + 1).ToString();
            btnThucHien.Text = "Tiếp Nhận";

            LayDanhSachNXB();
            LayDanhSachTacGia();
            LayDanhSachTheLoai();
            List<TheLoaiDTO> listTheLoai = TheLoaiService.LayDanhSachTheLoai();
            cmbTheLoai.DataSource = listTheLoai;
            cmbTheLoai.DisplayMember = "TenTheLoai";
            cmbTheLoai.ValueMember = "MaTheLoai";

            

            List<NhaXuatBanDTO> listNXB = NhaXuatBanServices.LayDanhSachNXB();
            cmbNXB.DataSource = listNXB;
            cmbNXB.DisplayMember = "TenNhaXuatBan";
            cmbNXB.ValueMember = "MaNhaXuatBan";
        }

        private void rdbCapNhat_CheckedChanged(object sender, EventArgs e)
        {
            grbQuanLySach.Enabled = true;
            btnThucHien.Enabled = true;
            btnNhapLai.Enabled = true;

            txtMaSoSach.ReadOnly = false;
            txtTenSach.ReadOnly = false;
            txtNamXB.ReadOnly = false;
            txtTriGia.ReadOnly = false;
            cmbNXB.Enabled = true;
            cmbTacGia.Enabled = true;
            cmbTheLoai.Enabled = true;

            btnThucHien.Text = "Cập Nhật";
        }

        private void rdbXoa_CheckedChanged(object sender, EventArgs e)
        {
            grbQuanLySach.Enabled = true;
            btnThucHien.Enabled = true;
            btnNhapLai.Enabled = true;

            txtMaSoSach.ReadOnly = false;
            txtTenSach.ReadOnly = true;
            txtNamXB.ReadOnly = true;
            txtTriGia.ReadOnly = true;
            cmbNXB.Enabled = false;
            cmbTacGia.Enabled = false;
            cmbTheLoai.Enabled = false;

            btnThucHien.Text = "Xóa";
        }

        //private string TaoMaSoSach()
        //{
        //    string maso = TheLoaiService.LayThongTinTheLoai(Convert.ToInt32(cmbTheLoai.SelectedValue)).TenTat;

        //}

        private void btnThucHien_Click(object sender, EventArgs e)
        {
            if (rdbTiepNhan.Checked == true)
            {
                SachDTO sachDTO = new SachDTO();



                sachDTO.MaSoSach = txtMaSoSach.Text; ;
                sachDTO.TenSach = txtTenSach.Text;
                sachDTO.NamXuatBan = Convert.ToInt32(txtNamXB.Text);
                sachDTO.MaTacGia = Convert.ToInt32(cmbTacGia.SelectedValue);
                sachDTO.MaTheLoai = Convert.ToInt32(cmbTheLoai.SelectedValue);
                sachDTO.NhaXuatBan = Convert.ToInt32(cmbNXB.SelectedValue);
                sachDTO.NgayNhap = Convert.ToDateTime(dtpkNgayTiepNhan.Text);
                sachDTO.DonGia = Convert.ToInt32(txtTriGia.Text);


                //************
                sachDTO.NguoiTiepNhan = 2;

                SachService.ThemSach(sachDTO);
                MessageBox.Show("Đã thêm thành công");
            }
        }

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            if (rdbTiepNhan.Checked == true)
            {
                txtTenSach.Text = "";
                txtNamXB.Text = "";
                txtTriGia.Text = "";
                LayDanhSachNXB();
                LayDanhSachTacGia();
                LayDanhSachTheLoai();
            }
            else if (rdbCapNhat.Checked == true)
                txtMaSoSach.Text = "";
            else
                txtMaSoSach.Text = "";
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
